# 定义变量，使用默认值
# 获取当前日期
DATE=$(date +%F)
IMAGE_NAME="shm_$DATE"

# Dockerfile 的路径
DOCKERFILE_PATH ?= docker/DockerFile
# Docker Compose 文件路径
DOCKER_COMPOSE ?= docker-compose.yml
# Docker 构建上下文，默认是当前目录
BUILD_CONTEXT ?= .


.PHONY: build  deploy rollback clean

# 构建 Docker 镜像
build:
	@echo "Building Docker image shm_$(date +F%)..."
	docker build -t "shm_$(date +F%)" --no-cache -f $(DOCKERFILE_PATH)  $(BUILD_CONTEXT)


# 部署应用，使用 docker-compose
deploy: build
	@echo "stop service..."
	docker-compose -f $(DOCKER_COMPOSE) down svc
	@echo "Deploying application with $(DOCKER_COMPOSE)..."
	docker-compose -f $(DOCKER_COMPOSE) up -d svc
	docker-compose -f $(DOCKER_COMPOSE) logs -f

run:
	@echo "=====> Deploying application with $(DOCKER_COMPOSE)..."
	docker-compose -f $(DOCKER_COMPOSE) down svc
	docker-compose -f $(DOCKER_COMPOSE) up -d svc
	docker-compose -f $(DOCKER_COMPOSE) logs -f


# 回滚到上一个版本
rollback:
	@echo "Rolling back to previous version..."
	docker-compose -f $(DOCKER_COMPOSE) down svc
	docker-compose -f $(DOCKER_COMPOSE) up -d svc

# 清理未使用的 Docker 镜像和容器
clean:
	@echo "Cleaning up unused Docker images and containers..."
	docker system prune -f

install:
	pip3 install -U pip
	pip3 install -r requirements.txt
